package com.bibrApp.difu.actvity.notice_detail_mvp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.NoticeDetailResBody;
import com.bibrApp.difu.actvity.notice_center_mvp.NoticeCenterActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;

public class NoticeDetailActivity extends BaseActivity<NoticeDetailPresenter> implements INoticeDetailView {

    @BindView(R.id.tv_notice_header)
    TextView tvHeader;
    @BindView(R.id.notice_content_Wv)
    WebView webView;
    @BindView(R.id.return_iv)
    View ivReturn;
    @BindView(R.id.notice_back_help_tv)
    TextView tvBackNoticeCenter;
    private ImmersionBar mImmersionBar;
    private int mType;//0从首页公告进入,1从消息中心进入,2从注册页面进入

    @Override
    protected void initPresenter() {
        presenter = new NoticeDetailPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice_detail;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {

        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.black_f0)
                .navigationBarColor(R.color.black_f0)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();

        mType = getIntent().getIntExtra("type", 0);
        //从注册页面注册协议进入，不显示返回消息中心按钮
        tvBackNoticeCenter.setVisibility(mType == 2 ? View.GONE : View.VISIBLE);


        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);

        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        //解决图片不显示
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.getSettings().setBlockNetworkImage(false);

        /*设置编码方式*/
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        /*设置背景透明*/
//        noticeWv.setBackgroundColor(Color.argb(0, 0, 0, 0));
        /*设置支持js*/
        webView.getSettings().setJavaScriptEnabled(true);
        /*设置支持缩放*/
        webView.getSettings().setSupportZoom(true);

        webView.setWebViewClient(new ReWebViewClient());
        webView.setWebChromeClient(new WebChromeClient());


        String id = getIntent().getStringExtra("id");
        presenter.getNoticeDetail(id);
        setOnClick();
    }

    class ReWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
//            mProgress.show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //           mProgress.dismiss();
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    }

    private void setOnClick() {
        ivReturn.setOnClickListener(v -> finish());
        tvBackNoticeCenter.setOnClickListener(v -> {
                    if (mType == 0) {
                        startActivity(new Intent(this, NoticeCenterActivity.class));
                    } else if (mType == 1 || mType == 2) {
                        finish();
                    }
                }
        );
    }


    @Override
    public void successOfNotice(NoticeDetailResBody data) {
        tvHeader.setText(data.getTitle());
        webView.loadDataWithBaseURL(null, data.getContents(), "text/html", "utf-8", null);
    }

    @Override
    public void error(String error) {
        ToastUtils.ShowToast(this, error);
    }


    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
        if (webView != null) {
            webView.stopLoading();
            webView.getSettings().setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.removeAllViews();
            webView.destroy();
        }
    }
}
